package com.bjc.core.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bjc.base.bean.ResultVO;
import com.bjc.base.core.ResultGenerator;
import com.bjc.base.core.annotation.PermissionPath;
import com.bjc.core.bean.request.DimensionReqVO;
import com.bjc.core.bean.response.DimensionVO;
import com.bjc.core.service.IDimensionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 维度控制器
 * 
 * @author 许自辉
 * @date 2017年8月21日
 */

//@SpringBootConfiguration
@RestController
@RequestMapping("api/cloud")
@PermissionPath("系统管理/维度")
@Api(value = "dimension-api", tags = { "维度相关接口" })
public class DimensionController {

	@Autowired
	private IDimensionService dimensionService;

	@ApiOperation("分页查询维度")
	@RequiresPermissions("dimension:findPage")
	@GetMapping("dimension/findPage")
	public ResultVO getDimensionPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
			DimensionReqVO dimensionReqVO) {
		PageHelper.startPage(pageNum, pageSize);
		List<DimensionVO> dimensionVOs = dimensionService.getList(dimensionReqVO);
		return ResultGenerator.getSuccess(new PageInfo<DimensionVO>(dimensionVOs));
	}

	@ApiOperation("添加维度")
	@RequiresPermissions("dimension:create")
	@PostMapping("dimension/create")
	public ResultVO createDimension(@RequestBody DimensionReqVO dimensionReqVOs) {
		int count = dimensionService.insert(dimensionReqVOs);
		if (count == -1) {
			return ResultGenerator.getFail("该维度编码已经存在!");
		}
		return ResultGenerator.getInsertSuccess(count);
	}

	@ApiOperation("更新维度")
	@RequiresPermissions("dimension:update")
	@PutMapping("dimension/update")
	public ResultVO updateDimension(@RequestBody DimensionReqVO dimensionReqVOs) {
		int count = dimensionService.update(dimensionReqVOs);
		return ResultGenerator.getUpdateSuccess(count);
	}

	@ApiOperation("删除维度")
	@RequiresPermissions("dimension:delete:id")
	@DeleteMapping("dimension/delete/{id}")
	public ResultVO deleteDimension(@ApiParam(value = "维度id", required = true) @PathVariable("id") int id) {
		int count = dimensionService.deleteById(id);
		if (count == -1) {
			return ResultGenerator.getFail("请先删除所有数据范围!");
		}
		return ResultGenerator.getDeleteSuccess(count);
	}
}
